<template>
  <j-modal :title="title" :width="600" :visible="visible" :confirmLoading="loading" switchFullscreen
    @cancel="handleCancel" @ok="handleOk">
    <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="{ span: 5 }" :wrapper-col="{ span: 18 }">
      <a-form-model-item label="身份证号" prop="identification">
        <a-input v-model="form.identification" placeholder="请输入" @change="onChange" />
      </a-form-model-item>
      <a-form-model-item label="成员类型" prop="memberType">
        <a-select v-model="form.memberType" placeholder="请选择">
          <a-select-option value="1">组长</a-select-option>
          <a-select-option value="2">成员</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="姓名" prop="name">
        <a-input v-model="form.name" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="性别" prop="gender">
        <a-select v-model="form.gender" placeholder="请选择">
          <a-select-option value="1">男</a-select-option>
          <a-select-option value="2">女</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="年龄" prop="age">
        <a-input v-model="form.age" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="职务/身份" prop="post">
        <a-select v-model="form.post" placeholder="请选择" allowClear style="width:100%;">
          <a-select-option v-for="(item, key) in jobArr" :key="key" :value="item.value">{{ item.title }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="联系方式" prop="telphone">
        <a-input v-model="form.telphone" placeholder="请输入" />
      </a-form-model-item>
    </a-form-model>
  </j-modal>
</template>
<script>
import { getDictItemsFromCache } from '@/api/api'
import * as groupApi from '@/api/sjsk/groupManage.js'
export default {
  name: 'AddModal',
  props: ['title'],
  data() {
    return {
      visible: false,
      jobArr: getDictItemsFromCache('groupPost'),
      form: {},
      rules: {
        name: [{ required: true, message: '请输入' }],
        memberType: [{ required: true, message: '请选择' }],
        gender: [{ required: true, message: '请选择' }],
        age: [{ required: true, message: '请输入' }],
        identification: [{ required: true, message: '请输入' }],
        post: [{ required: true, message: '请选择' }],
        telphone: [{ required: true, message: '请输入' }],
      },
      loading: false,
      isAdd: true
    }
  },
  async created() {
  },
  methods: {
    show(type, groupId, record) {
      this.visible = true
      this.form = {}
      this.form.groupId = groupId //新增，传入小组id
      if (type == 1) {
        this.isAdd = true
      } else { //编辑，回填全部信息
        this.isAdd = false
        this.form = Object.assign({}, record) //拷贝一份数据，不然影响列表
        this.form.gender = record.gender.toString()
        this.form.memberType = record.memberType.toString()
      }
    },
    onChange(e) {
      let pageFilter = {
        pageNo: 1,
        pageSize: 10,
        identification: this.form.identification
      }
      this.refreshTable(pageFilter)
    },
    refreshTable(pageFilter) {
      groupApi.pageNewGroup(pageFilter).then((res) => {
        if (res.success) {
          if (res.result) {
            let val = res.result.records[0]
            this.$set(this.form, 'id', val.id)
            this.$set(this.form, 'name', val.name)
            this.$set(this.form, 'gender', val.gender)
            this.$set(this.form, 'age', val.age)
            this.$set(this.form, 'post', val.post)
            this.$set(this.form, 'telphone', val.telphone)
          }
        }
      })
    },
    handleCancel() {
      this.visible = false
    },
    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          let api = ""
          if (this.isAdd) {
            api = groupApi.addWorkGroup(this.form)
          } else {
            api = groupApi.addGroup(this.form)
          }
          api.then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok');
              this.visible = false
              this.loading = false
            } else {
              this.loading = false
              this.$message.warn(res.message)
            }
          })
            .catch(e => {
              this.loading = false
              this.$message.warn('请求失败！')
            })
        }
      })
    },
  },
}
</script>
<style lang="less" scoped>
/deep/.ant-form-item {
  margin-bottom: 10px;
}
</style>
